# 设置错误时停止执行
$ErrorActionPreference = "Stop"

# 版本号
$version = "1.0.0"

# 创建发布目录
$releaseDir = "release"
$packageName = "apilogger-v$version-windows"
$packageDir = Join-Path $releaseDir $packageName

# 清理旧的发布目录
if (Test-Path $releaseDir) {
    Remove-Item $releaseDir -Recurse -Force
}

# 创建新的发布目录
New-Item -ItemType Directory -Path $packageDir | Out-Null

# 运行测试
Write-Host "Running tests..." -ForegroundColor Green
cargo test
if ($LASTEXITCODE -ne 0) {
    Write-Host "Tests failed!" -ForegroundColor Red
    exit 1
}

# 构建发布版本
Write-Host "Building release version..." -ForegroundColor Green
cargo build --release
if ($LASTEXITCODE -ne 0) {
    Write-Host "Build failed!" -ForegroundColor Red
    exit 1
}

# 复制文件到发布目录
Write-Host "Copying files..." -ForegroundColor Green
Copy-Item "target/release/apilogger.exe" $packageDir
Copy-Item "config.yaml" $packageDir
Copy-Item "README.md" $packageDir

# 创建目录
New-Item -ItemType Directory -Path (Join-Path $packageDir "json_bodies") | Out-Null

# 创建压缩包
Write-Host "Creating zip archive..." -ForegroundColor Green
Compress-Archive -Path $packageDir/* -DestinationPath "$releaseDir/$packageName.zip"

Write-Host "Release package created successfully!" -ForegroundColor Green
Write-Host "Location: $releaseDir/$packageName.zip" -ForegroundColor Yellow 